/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hibernate.model.dao;

import hibernate.pojo.Chitietsanpham;
import hibernate.pojo.Sanpham;
import hibernate.util.HibernateUtil;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author Administrator
 */
public class SanPhamDAO 
{
    //<editor-fold defaultstate="collapsed" desc="1. HQL - Lấy danh sách sản phẩm">

    public static List<Sanpham> LayDanhSachSanPhamHQL() {
        List<Sanpham> arrSanPham = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            int soSanPhamTrenTrangChu = ThamSoDAO.LaySoSanPhamTrenTrangChu();
            String hql = "select sp from Sanpham sp where sp.tinhTrangKhuyenMai=1 and sp.tinhTrangXoa=1";
            Query query = session.createQuery(hql);
            query.setMaxResults(soSanPhamTrenTrangChu);
            arrSanPham = query.list();
        } catch (HibernateException ex) {
            System.out.println(ex);
        } finally {
            session.close();
        }
        return arrSanPham;
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="2. HQL - Lấy chi tiết sản phẩm">
    public static Chitietsanpham LayChiTietSanPhamHQL(int maSanPham) 
    {
        Chitietsanpham chitietsp = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try 
        {
            String hql = "select ctsp from Chitietsanpham ctsp where ctsp.sanpham.maSanPham=:maSanPham";
            Query query = session.createQuery(hql);
            query.setInteger("maSanPham", maSanPham);
            Object ob = query.uniqueResult();
            chitietsp = (Chitietsanpham) ob;
            session.close();
        } catch (HibernateException ex) 
        {
            System.out.println(ex);
        }
        return chitietsp;
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="3. Lấy sản phẩm by ID">
    public static Sanpham getProductByID(int maSanPham) {
        Sanpham sanPham = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            sanPham = (Sanpham) session.get(Sanpham.class, maSanPham);
        } catch (HibernateException hex) {
            System.err.print(hex);
        } finally {
            session.close();
        }
        return sanPham;
    }

    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="4. Lấy danh sách Deal trước đây.">
    public static List<Sanpham> LayDanhSachDealTruocDayHQL() 
    {
        List<Sanpham> arrSanPham = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            int soSanPhamTrenTrangChu = ThamSoDAO.LaySoSanPhamTrenTrangChu();
            String hql = "select sp from Sanpham sp where sp.tinhTrangKhuyenMai=0 and sp.tinhTrangXoa=1";
            Query query = session.createQuery(hql);
            query.setMaxResults(soSanPhamTrenTrangChu);
            arrSanPham = query.list();
            //session.close();
        } catch (HibernateException ex) {
            System.out.println(ex);
        }
        return arrSanPham;
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="5. Lấy danh sách Deal Sự Kiện.">
    public static List<Sanpham> LayDanhSachDealSuKienHQL(int maLoaiSanPham) {
        List<Sanpham> arrSanPham = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "select sp from Sanpham sp where sp.loaisanpham.maLoaiSanPham=:maLoaiSanPham and sp.tinhTrangXoa=1";
            Query query = session.createQuery(hql);
            query.setInteger("maLoaiSanPham", maLoaiSanPham);
            arrSanPham = query.list();
            session.close();
        } catch (HibernateException ex) {
            System.out.println(ex);
        }
        return arrSanPham;
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="6.search zingdeal">
    //public static List<Sanpham> searchProduct(String tenSanPham, String tenLoaiSanPham, Date ngayBatDauKhuyenMai, Date ngayHetHanKhuyenMai)
    public static List<Sanpham> searchProduct(String tenSanPham, String tenLoaiSanPham, String ngayBatDauKhuyenMai, String ngayHetHanKhuyenMai)
    {
        List<Sanpham> productList = null;
        Session session = (Session) HibernateUtil.getSessionFactory().openSession();
        try 
        {
            String hql = "from Sanpham sp where sp.tinhTrangKhuyenMai='0' ";

            if (!(tenSanPham.equals(""))) 
            {
                hql += " and sp.tenSanPham like:tenSanPham";
            }

            if (!(tenLoaiSanPham.equals(""))) 
            {
                hql += " and sp.loaisanpham.tenTrang =:tenTrang";
            }

            if (ngayBatDauKhuyenMai != null) 
            {
                hql += " and sp.ngayHetHanKhuyenMai >=:ngayHetHanKhuyenMai";
            }

            if (ngayHetHanKhuyenMai != null) 
            {
                hql += " and sp.ngayHetHanKhuyenMai <=:ngayHetHanKhuyenMai";
            }

            Query query = session.createQuery(hql);
            if (!(tenSanPham.equals(""))) 
            {
                query.setString("tenSanPham", "%" + tenSanPham + "%");
            }
            if (!(tenLoaiSanPham.equals(""))) 
            {
                query.setString("tenTrang", tenLoaiSanPham);
            }
            if (ngayBatDauKhuyenMai != null) 
            {
                SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
                Date d = sdf.parse(ngayBatDauKhuyenMai);
                sdf = new SimpleDateFormat("yyyy-MM-dd");
                String dd = sdf.format(d);
                query.setString("ngayHetHanKhuyenMai", "'"+dd+"'");
            }
            if (ngayHetHanKhuyenMai != null) 
            {
                SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
                Date d = sdf.parse(ngayHetHanKhuyenMai);
                sdf = new SimpleDateFormat("yyyy-MM-dd");
                String dd = sdf.format(d);
                query.setString("ngayHetHanKhuyenMai", "'"+dd+"'");
            }
            productList = query.list();

        } catch (Exception ex) 
        {
            System.out.println(ex);
        }
        return productList;
    }//end of function
    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="2. Lấy ngày hệ thống">
//    public static Date LayNgayHeThong()
//    {
//        Calendar cal = Calendar.getInstance();
//        int Nam = cal.get(Calendar.YEAR);
//        int Thang = cal.get(Calendar.MONTH) + 1;
//        int Ngay = cal.get(Calendar.DATE);  
//
//        String sNam = String.valueOf(Nam);
//        String sThang = String.valueOf(Thang);
//        String sNgay = String.valueOf(Ngay);
//        if(Ngay<10)
//        {
//            String temp = "0"+sNgay;
//            sNgay = temp;
//        }
//        if(Thang<10)
//        {
//            String temp = "0"+sThang;
//            sThang = temp;
//        }
//        String fm = String.format("%s-%s-%s", sNam,sThang,sNgay);
//        String NgayLapHoaDon = fm;
//        Date ngay = Date.valueOf(NgayLapHoaDon);
//        return ngay;
//    }
    //</editor-fold>
}
